package org.jeecg.modules.message.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.dromara.sms4j.api.SmsBlend;
import org.dromara.sms4j.api.entity.SmsResponse;
import org.dromara.sms4j.core.factory.SmsFactory;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.demo.pcl.entity.SmsSendLog;
import org.jeecg.modules.demo.pcl.mapper.SmsSendLogMapper;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.LinkedHashMap;
import java.util.List;

/**
 * 发送消息任务
 * @author: jeecg-boot
 */

@Slf4j
public class SmsAndonJob implements Job {

	@Autowired private SmsSendLogMapper smsSendLogMapper;


	@Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
		//条件查询 所有send_status 是0的
	 	List<SmsSendLog> list =smsSendLogMapper.selectList(new QueryWrapper<SmsSendLog>().eq("send_status", "0"));
		//循环查到的list集合
		for (SmsSendLog smsSendLog : list) {
			//发送短信
			System.out.println("根据configId发送短信测试");
			// 以下为示例代码，实际项目中需要根据具体实现进行调整
			SmsBlend smsBlend = SmsFactory.getSmsBlend("tx1");
			//读取短信内容
			String content = smsSendLog.getContent();
			LinkedHashMap<String, String> messageParams = null;
			try {
				messageParams = new ObjectMapper().readValue(content, new TypeReference<LinkedHashMap<String, String>>(){});
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
			//将JSON格式的content转换为LinkedHashMap
			String phoneNumber = smsSendLog.getPhoneNumber();
			String templateCode = smsSendLog.getTemplateCode();
			// 读取模板内容
			SmsResponse smsResponse = smsBlend.sendMessage(phoneNumber, templateCode, messageParams);
			LinkedHashMap<String, String> responseData = new LinkedHashMap<>();
			try {
				// 将返回的 JSON 字符串转换为 LinkedHashMap
				responseData = new ObjectMapper().readValue(smsResponse.getData().toString(), new TypeReference<LinkedHashMap<String, String>>(){});
			} catch (JsonProcessingException e) {
				log.error("解析短信返回结果失败", e);
			}
			if (smsResponse.isSuccess()) {
				// 解析返回数据
				smsSendLog.setSendStatus(1);
				smsSendLog.setResponseCode(responseData.getOrDefault("Code", ""));
				smsSendLog.setResponseMsg(responseData.getOrDefault("Message", ""));
				smsSendLog.setBizId(responseData.getOrDefault("BizId", ""));
				smsSendLog.setSendTime(DateUtils.getTimestamp());
				smsSendLog.setUpdatedAt(DateUtils.getTimestamp());
				smsSendLog.setRemark("发送成功");
				smsSendLogMapper.updateById(smsSendLog);
			} else {
				smsSendLog.setSendStatus(2); // 直接使用整数
				smsSendLog.setResponseCode(responseData.getOrDefault("Code", ""));
				smsSendLog.setResponseMsg(responseData.getOrDefault("Message", ""));
				smsSendLog.setBizId(responseData.getOrDefault("BizId", ""));
				smsSendLog.setSendTime(DateUtils.getTimestamp());
				smsSendLog.setUpdatedAt(DateUtils.getTimestamp());
				smsSendLog.setRemark("发送失败");
				smsSendLogMapper.updateById(smsSendLog);
			}
			log.info("发送结果：" + smsResponse);
		}

	}

}
